unit adsearchmain;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  System.WideStrings,
  ComCtrls, StdCtrls, ExtCtrls, ADSISearch;

type
  TfrmMain = class(TForm)
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    edtObjCategory: TEdit;
    edtFilter: TEdit;
    edtAttr: TEdit;
    btnSearch: TButton;
    chkFirstOnly: TCheckBox;
    Memo1: TMemo;
    Label4: TLabel;
    edtBasePath: TEdit;
    ADSISearch1: TADSISearch;
    btn1: TButton;
    procedure btnSearchClick(Sender: TObject);
    procedure btn1Click(Sender: TObject);

  private
    { Private declarations }

  public
    { Public declarations }
  end;

var
  frmMain: TfrmMain;


(*============================================================================*)
(*                           IMPLEMENTATION                                   *)
(*============================================================================*)

implementation

{$R *.DFM}

uses
  ActiveDs_TLB,
  ComObj;

const
  S_ADS_ERRORSOCCURRED = $00005011;
  S_ADS_NOMORE_ROWS    = $00005012;
  S_ADS_NOMORE_COLUMNS = $00005013;

  E_ADS_BAD_PATHNAME            = $80005000;
  E_ADS_INVALID_DOMAIN_OBJECT   = $80005001;
  E_ADS_INVALID_USER_OBJECT     = $80005002;
  E_ADS_INVALID_COMPUTER_OBJECT = $80005003;
  E_ADS_UNKNOWN_OBJECT          = $80005004;
  E_ADS_PROPERTY_NOT_SET        = $80005005;
  E_ADS_PROPERTY_NOT_SUPPORTED  = $80005006;
  E_ADS_PROPERTY_INVALID        = $80005007;
  E_ADS_BAD_PARAMETER           = $80005008;
  E_ADS_OBJECT_UNBOUND          = $80005009;
  E_ADS_PROPERTY_NOT_MODIFIED   = $8000500A;
  E_ADS_PROPERTY_MODIFIED       = $8000500B;
  E_ADS_CANT_CONVERT_DATATYPE   = $8000500C;
  E_ADS_PROPERTY_NOT_FOUND      = $8000500D;
  E_ADS_OBJECT_EXISTS           = $8000500E;
  E_ADS_SCHEMA_VIOLATION        = $8000500F;
  E_ADS_COLUMN_NOT_SET          = $80005010;
  E_ADS_INVALID_FILTER          = $80005014;

procedure TfrmMain.btn1Click(Sender: TObject);
var
  slTemp: TWideStringList;
  ix: Integer;
  curLine: string;
  sADHomeDriveUncPath, sADHomeDriveLetter: string;

begin
  try
    //ADSISearch1.Filter := WideString('samaccountname=' + GetUserFromWindows());

    //ADSISearch1.Filter := WideString('samaccountname=sopilko');

    try
      ADSISearch1.Search;
      slTemp := ADSISearch1.GetFirstRow();
    except
      //uh-oh, this is a problem, get out of here
      // --- must not have been able to talk to AD
      // --- could be the user recently changed pwd and is logged in with
      //      their cached credentials
      // just suppress this exception

      //bHomeDriveMappingFailed := True;
      //Result := bSuccess;
      Exit;
    end;

    while (slTemp <> nil) do
    begin
      for ix := 0 to slTemp.Count - 1 do
      begin
        curLine := AnsiUpperCase(slTemp[ix]);
        {
        if AnsiStartsStr('HOMEDIRECTORY', curLine) then
        begin
          sADHomeDriveUncPath := AnsiReplaceStr(curLine, 'HOMEDIRECTORY=', '');
          //sADHomeDriveUncPath := slTemp[ix];
        end
        else if AnsiStartsStr('HOMEDRIVE', curLine) then
        begin
          sADHomeDriveLetter := AnsiReplaceStr(curLine, 'HOMEDRIVE=', '');
          //sADHomeDriveLetter := slTemp[ix];
        end;
        }
      end;

      FreeAndNil(slTemp);
      slTemp := ADSISearch1.GetNextRow();
    end;
  except
    //suppress this exception
    //bHomeDriveMappingFailed := True;
    Exit;
  end;
end;

procedure TfrmMain.btnSearchClick(Sender: TObject);
var
  ix : Integer;
  slTemp : TWideStringList;
begin
  with ADSISearch1 do begin
    BasePath := WideString(edtBasePath.Text);

    if chkFirstOnly.Checked then
      MaxRows := 1
    else
      MaxRows := -1;

    Attributes := WideString(edtAttr.Text);
    Filter := WideString(edtFilter.Text);
    //ObjectCategory := WideString(edtObjCategory.Text);

    Memo1.Lines.Clear;

    Search;

    slTemp := GetFirstRow();
    while (slTemp <> nil) do begin
      for ix := 0 to slTemp.Count-1 do begin
        Memo1.Lines.Add(slTemp[ix]);
      end;
      Memo1.Lines.Add(StringOfChar('-', 60));

      FreeAndNil(slTemp);
      slTemp := GetNextRow();
    end;
  end;
end;

end.

